package be.kuleuven.thesis.parsers;
//*package org.biojava3.genome.parsers.gff;


import java.io.*;
import java.util.*;
import java.util.logging.Logger;

import org.biojava3.genome.parsers.gff.Location;

public class MyGeneIDGFF2Reader {

	private static final Logger log = Logger.getLogger(MyGeneIDGFF2Reader.class.getName());

    /**
     * Read a file into a FeatureList. Each line of the file becomes one Feature object.
     *
     * @param filename The path to the GFF file.
     * @return A FeatureList.
     * @throws IOException Something went wrong -- check exception detail message.
     */
    public static MyFeatureList read(String filename) throws IOException {
        log.info("Gff.read(): Reading " + filename);

        MyFeatureList features = new MyFeatureList();
        BufferedReader br = new BufferedReader(new FileReader(filename));

        String s;
        for (s = br.readLine(); null != s; s = br.readLine()) {
            s = s.trim();

            if (s.length() > 0) {
                if (s.charAt(0) == '#') {
                    //ignore comment lines
                } else {

                    MyFeatureI f = parseLine(s);
                    if (f != null) {
                        features.add(f);
                    }
                }
            }

        }

        br.close();
        return features;
    }

    /**
     * create Feature from line of GFF file
     */
    private static MyFeature parseLine(String s) {
        //FIXME update to use regex split on tabs
        //FIXME better errors on parse failures

        int start = 0;
        int end = 0;

        start = end;
        end = s.indexOf('\t', start);
        String seqname = s.substring(start, end).trim();

        start = end + 1;
        end = s.indexOf('\t', start);
        String source = s.substring(start, end).trim();

        start = end + 1;
        end = s.indexOf('\t', start);
        String type = s.substring(start, end);

        start = end + 1;
        end = s.indexOf('\t', start);
        String locStart = s.substring(start, end);

        start = end + 1;
        end = s.indexOf('\t', start);
        String locEnd = s.substring(start, end);

        Double score;
        start = end + 1;
        end = s.indexOf('\t', start);
        try {
            score = Double.parseDouble(s.substring(start, end));
        } catch (Exception e) {
            score = 0.0;
        }

        start = end + 1;
        end = s.indexOf('\t', start);
        char strand = s.charAt(end - 1);

        //added by scooter willis to deal with glimmer predictions that
        //have the start after the end but is a negative strand
        int locationStart = Integer.parseInt(locStart);
        int locationEnd = Integer.parseInt(locEnd);
        if(locationStart > locationEnd){
            int temp = locationStart;
            locationStart = locationEnd;
            locationEnd = temp;
           
        }
        Location location = Location.fromBio(locationStart, locationEnd, strand);

        assert (strand == '-') == location.isNegative();

        int frame;
        start = end + 1;
        end = s.indexOf('\t', start);
        try {
            frame = Integer.parseInt(s.substring(start, end));
        } catch (Exception e) {
            frame = -1;
        }

        //grab everything until end of line (or # comment)
        start = end + 1;
        end = s.indexOf('#', start);
        
        String attributes = null;
        if (end < 0) {
            attributes = new String(s.substring(start));
        } else {
            attributes = new String(s.substring(start, end));
        }
        //System.out.println(attributes);
        //need to add in attribute assignment for geneid where it just provides a gene name and will make it gtf like
        //*attributes = "gene_id " + '"' + attributes + '"' + ";";
        return new MyFeature(seqname, source, type, location, score, frame, attributes);

    }

    /**
     * Write features in FeatureList to file. Each Feature becomes one line in the file.
     * The userMap() data in the features is not written to file.
     *
     * @param features The list of features to write.
     * @param filename The path to the file.
     * @throws IOException Something went wrong -- check exception detail message.
     */
    public static void write(MyFeatureList features, String filename) throws IOException {
        log.info("Writing " + filename);

        BufferedWriter bw = new BufferedWriter(new FileWriter(filename));

        ListIterator<MyFeatureI> iter = features.listIterator();
        while (iter.hasNext()) {
            MyFeature feature = (MyFeature) iter.next();
            writeLine(feature, bw);
        }

        bw.close();
    }

    private static void writeLine(MyFeature f, BufferedWriter bw) throws IOException {
        String s = f.seqname() + '\t';
        s += f.source() + '\t';
        s += f.type() + '\t';

        s += f.location().bioStart() + "\t";
        s += f.location().bioEnd() + "\t";
        s += Double.toString(f.score()) + "\t";
        s += f.location().bioStrand() + "\t";

        if (f.frame() == -1) {
            s += ".\t";
        } else {
            s += f.frame() + "\t";
        }

        s += f.attributes();

        bw.write(s);
        bw.newLine();
    }


    public static void main(String args[]) throws Exception {

        MyFeatureList listGenes = MyGeneIDGFF2Reader.read("/Users/Scooter/scripps/dyadic/analysis/454Scaffolds/genemark_hmm.gtf");
        System.out.println("Features");
        for(MyFeatureI feature : listGenes){
            System.out.println(feature);
        }
//        System.out.println(listGenes);
        //	GeneMarkGTF.write( list, args[1] );
    }

}
